using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoreData;
using EntityData;
using Helper;
using System.Web.Mvc;
namespace Services
{

    public partial class MerchantService
    {
       readonly IMerchantEntity entity;
       readonly IPlansEntity PlanEntity;
       readonly IUserEntity UserEntity;
       public MerchantService(IMerchantEntity entity,IPlansEntity planentity,IUserEntity userentity)
       {
           this.entity = entity;
           this.PlanEntity = planentity;
           this.UserEntity = userentity;
       }

       #region Main Method

       /// <summary>
       /// Save 
       /// </summary>
       /// <param name="_model"></param>
       /// <returns></returns>
       public long Save(object _model)
       {
           try
           {
               var obj = ((Merchant)_model);
               var id = obj.ID;
               if (id == 0)
               {

                   if (obj.Logo == null || obj.Logo == "")
                   {
                       obj.Logo = "noimage.png";
                   }
                   return entity.Save(obj, Table.Merchant.ToString());
               }
               else
               {
                   if (obj.Logo == null || obj.Logo == "")
                   {
                       obj.Logo = GetByID(obj.ID).Logo;
                   }

                   return entity.Update(obj, Table.Merchant.ToString());
               }
           }
           catch { return -1; }
       }

       /// <summary>
       /// Get by ID
       /// </summary>
       /// <param name="_id"></param>
       /// <returns></returns>
       public CoreData.Merchant GetByID(long _id)
       {
           try
           {
               return entity.GetByCusTomSQL(String.Format(SQLCommand.GetAllMerchant, "Merchant.ID="+_id)).ToList()[0];
           }
           catch { return null; }
       }

       /// <summary>
       /// Get List
       /// </summary>
       /// <returns></returns>
       public IList<CoreData.Merchant> GetList()
       {
           try
           {
               return entity.GetByCusTomSQL(String.Format(SQLCommand.GetAllMerchant,"1=1")).ToList();
           }
           catch { return null; }

       }

       /// <summary>
       /// Get List with Linq
       /// </summary>
       /// <returns></returns>
       public IList<CoreData.Merchant> GetListByLINQ(Func<CoreData.Merchant, Boolean> _where)
       {
           try
           {
               return entity.GetMany(_where, Table.Merchant.ToString()).ToList();
           }
           catch { return null; }
       }
       /// <summary>
       /// Get One with Linq
       /// </summary>
       /// <returns></returns>
       public CoreData.Merchant GetOneByLINQ(Func<CoreData.Merchant, Boolean> _where)
       {
           try
           {
               return entity.Get(_where, Table.Merchant.ToString());
           }
           catch { return null; }
       }

       /// <summary>
       /// Get List with search string
       /// </summary>
       /// <returns></returns>
       public IList<CoreData.Merchant> GetList(string _searchstring)
       {
           //search theo tieu chi nao do         
           try
           {
               return entity.GetByCusTomSQL(String.Format(SQLCommand.GetAllMerchant, _searchstring)).ToList();
           }
           catch { return null; }
       }


       /// <summary>
       /// Delete
       /// </summary>
       /// <param name="_model"></param>
       /// <returns></returns>
       public bool Delete(object _model)
       {
           try
           {
               entity.Delete((CoreData.Merchant)_model, Table.Merchant.ToString());
               return true;
           }
           catch { return false; }
       }


       #endregion



        #region Other Method

       public List<SelectListItem> GetListPlan(int id)
       {
           var lst = new List<SelectListItem>();
           var list = PlanEntity.GetAll(Table.Plans.ToString());
           foreach (var c in list)
           {
               var item = new SelectListItem();
               item.Text = c.Name;
               item.Value = c.ID.ToString();
               if (id != 0)
                   item.Selected = c.ID == id;
               lst.Add(item);
           }
           return lst;
       }

       public List<SelectListItem> GetListUser(long id)
       {
           var lst = new List<SelectListItem>();
           var listcate = UserEntity.GetByCusTomSQL(String.Format(SQLCommand.GetUserIsMerchant));
           lst.Add(new SelectListItem() { 
                Selected = true,
                Text ="All",
                Value="0"
           });
           foreach (var c in listcate)
           {
               var item = new SelectListItem();
               item.Text = c.UserName;
               item.Value = c.ID.ToString();
               if (id != 0)
                   item.Selected = c.ID == id;
               lst.Add(item);
           }
           return lst;

       }

       public List<SelectListItem> GetListStatus(int id)
       {
           var lst = new List<SelectListItem>();
           DictionaryDefine listcate = new DictionaryDefine();
           foreach (var item in listcate.DicMerchantStatus)
           {
               var sitem = new SelectListItem();
               sitem.Value = item.Key.ToString();
               sitem.Text = item.Value;
               if (id != 0)
                   sitem.Selected = item.Key == id;
               lst.Add(sitem);
           }
           return lst;
       }
        #endregion 
    
    }
         
}
